package cc.ufinity.networkdiagnose.view;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends TabActivity implements OnCheckedChangeListener {
    /** Called when the activity is first created. */
	private RadioGroup radioGroup;
    private TabHost tabHost;
    private Intent ihome;
    private Intent itask;
    private Intent itest;
    private Intent icfg;
    
	private final static String TAB_TAG_HOME="tab_tag_home";
	private final static String TAB_TAG_TEST="tab_tag_test";
	private final static String TAB_TAG_TASK="tab_tag_task";
	private final static String TAB_TAG_CFG="tab_tag_cfg";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        radioGroup = (RadioGroup)this.findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(this);
        prepareIntent();
        setupIntent();
    }
     
	@Override
	public void onCheckedChanged(RadioGroup rGroup, int checkId) {
		switch(checkId){
		case R.id.radio_task:
			this.tabHost.setCurrentTabByTag(TAB_TAG_TASK);
			break;
		case R.id.radio_cfg:
			this.tabHost.setCurrentTabByTag(TAB_TAG_CFG);
			break;
		case R.id.radio_test:
			this.tabHost.setCurrentTabByTag(TAB_TAG_TEST);
			break;
		case R.id.radio_home:
			this.tabHost.setCurrentTabByTag(TAB_TAG_HOME);
			break;
		}
		
	}
	
	private void prepareIntent(){
		ihome = new Intent(this,HomeActivity.class);
		//ihome = new Intent(this,TestActivity.class);
		itask = new Intent(this,TaskActivity.class);
		itest = new Intent(this,TestActivity.class);
		icfg = new Intent(this,CfgActivity.class);
	}
	
	private void setupIntent(){
		tabHost = this.getTabHost();
		tabHost.addTab(buildTabSpec(TAB_TAG_TEST,R.string.meun_test,R.drawable.tether,itest));
		tabHost.addTab(buildTabSpec(TAB_TAG_HOME,R.string.meun_home,R.drawable.home,ihome));
		tabHost.addTab(buildTabSpec(TAB_TAG_TASK,R.string.meun_task,R.drawable.tasks,itask));
		tabHost.addTab(buildTabSpec(TAB_TAG_CFG,R.string.meun_cfg,R.drawable.settings,icfg));
	}
	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,final Intent content) {
		return this.tabHost.newTabSpec(tag).setIndicator(getString(resLabel),
				getResources().getDrawable(resIcon)).setContent(content);
	}
	
}
